package com.ferdict;

import java.net.URLEncoder;
import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity implements OnClickListener {
	private static final String tag = "LoginActivity";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		findViewById(R.id.login_button).setOnClickListener(this);
		findViewById(R.id.cancel_login_button).setOnClickListener(this);
	}

	// @Override
	public void onClick(View v) {
		Intent i = null;
		switch (v.getId()) {
		case R.id.login_button:
			String res = null;
			String token = null;

			String username = ((EditText) findViewById(R.id.edit_text_id_username))
					.getText().toString();
			String password = ((EditText) findViewById(R.id.edit_text_id_password))
					.getText().toString();
			if (null == username || username.length() == 0) {
				Toast t = Toast.makeText(this, "User name required!", Toast.LENGTH_SHORT);
				t.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
				t.show();
				break;
			}
			if (null == password || password.length() == 0) {
				Toast t = Toast.makeText(this, "Password required!", Toast.LENGTH_SHORT);
				t.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
				t.show();

			}
			try {

				ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
				nameValuePairs.add(new BasicNameValuePair("username",
						((EditText) findViewById(R.id.edit_text_id_username))
								.getText().toString()));
				nameValuePairs.add(new BasicNameValuePair("password",
						((EditText) findViewById(R.id.edit_text_id_password))
								.getText().toString()));
				res = RestClient.doPost(Constants.SERVER, Constants.PORT,
						Constants.LOGIN_URL, new UrlEncodedFormEntity(
								nameValuePairs));
				// + "?userName="
				// + URLEncoder
				// .encode(((EditText) findViewById(R.id.edit_text_id_username))
				// .getText().toString())
				// + "&password="
				// + URLEncoder
				// .encode(((EditText) findViewById(R.id.edit_text_id_password))
				// .getText().toString()));
				// "utf-8"));
				if (null != res) {
					JSONObject o = new JSONObject(res);
					token = o.getString("token");
				}
				if (null == token || 0 == token.length()) {
					Toast.makeText(this, "Login incorrect.  Please try again.",
							Toast.LENGTH_SHORT);
					break;
				}
			} catch (Exception e) {
				Log.e(tag, "Could not login!", e);
				break;
			}
			Intent resultIntent = new Intent();
			resultIntent.putExtra("token", token);
			setResult(RESULT_OK, resultIntent);
			finish();
			break;
		case R.id.cancel_login_button:
			finish();
			break;
		}
	}
}